<?php


namespace app\model;


use think\facade\Db;
use think\response\Json;
use \Exception as E;
class StoreFinanceModel
{
    private static $_baseTable = 'eb_admin'; // 管理员表

    /**
     * @param $input
     * @param $token
     * @return Json
     */
    public static function list($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [5]);
            if ($rtn instanceof Json) return $rtn;
            // 获取列表
            $table = Db::table(self::$_baseTable);
            $table->field('id, account, name, phone, status,FROM_UNIXTIME(create_time, "%Y-%m-%d %H:%i:%s") AS create_time');
            $map[] = ['store_id', '=',$rtn['store_id']];
            $map[] = ['type', '=', 6];
            $map[] = ['pid', '=', $rtn['id']];
            if ($input['name'] !== '') $map[] = ['name', 'like', "%{$input['name']}%"];
            $table->where($map);
            $count = $table->count();
            if ($count == 0 ) return jsonSuccess('查询成功', ['count'=> $count, 'list' => []]);
            $list = $table->order('create_time', 'desc')->page($input['page'], $input['limit'])->select();
            return jsonSuccess('查询成功', ['count'=> $count, 'list' => $list]);
        } catch (E $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /** 添加财务员
     * @param $input
     * @param $token
     * @return Json
     */
    public static function add($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [5]);
            if ($rtn instanceof Json) return $rtn;
            // 判断是否已经添加
            $info = Db::table(self::$_baseTable)->field('id')->where('account', $input['account'])->find();
            if ($info) return jsonFail('账号已存在，不能添加');
            // 添加财务员
            unset($input['repwd']);
            $input['store_id'] = $rtn['store_id'];
            $input['pwd'] = sha1($input['pwd']);
            $input['type'] = 6;
            $input['pid'] = $rtn['id'];
            $input['bank_no'] = '';
            $input['bank_name'] = '';
            $input['create_time'] = time();
            $rtn = Db::table(self::$_baseTable)->insert($input);
            if ($rtn) return jsonSuccess('添加成功');
            return jsonFail('添加失败');
        } catch (E $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /** 删除财务
     * @param $input
     * @param $token
     * @return Json
     */
    public static function delete($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [5]);
            if ($rtn instanceof Json) return $rtn;
            // 删除财务
            $map[] = ['store_id', '=', $rtn['store_id']];
            $map[] = ['pid', '=', $rtn['id']];
            $map[] = ['type', '=', 6];
            $rtn = Db::table(self::$_baseTable)->where($map)->delete($input['id']);
            if ($rtn == 1) return jsonSuccess('删除成功');
            return jsonFail('删除失败');
        } catch (E $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /** 获取单个信息
     * @param $input
     * @param $token
     * @return Json
     */
    public static function only($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [5]);
            if ($rtn instanceof Json) return $rtn;
            // 获取单个信息
            $map[] = ['id', '=', $input['id']];
            $map[] = ['store_id', '=', $rtn['store_id']];
            $map[] = ['pid', '=', $rtn['id']];
            $map[] = ['type', '=', 6];
            $rtn = Db::table(self::$_baseTable)->field('id,account,name,phone,status')->where($map)->find();
            if ($rtn) return jsonSuccess('获取成功', $rtn);
            return jsonFail('未找到信息');
        } catch (E $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /** 编辑财务
     * @param $input
     * @param $token
     * @return Json
     */
    public static function edit($input, $token)
    {
        $flag = false;
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [5]);
            if ($rtn instanceof Json) return $rtn;
            // 编辑财务
            $map[] = ['id', '=', $input['id']];
            $map[] = ['store_id', '=', $rtn['store_id']];
            $map[] = ['pid', '=', $rtn['id']];
            $map[] = ['type', '=', 6];
            $rtn = Db::table(self::$_baseTable)->where($map)->update($input);
            if ($rtn) return jsonSuccess('编辑成功');
            return jsonFail('编辑失败');
        } catch (E $e) {
            if ($flag == true) Db::rollback();
            return jsonFail('服务内部错误~');
        }
    }

    /** 修改密码
     * @param $input
     * @param $token
     * @return Json
     */
    public static function changePwd($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [5]);
            if ($rtn instanceof Json) return $rtn;
            // 获取单个信息
            unset($input['chkpwd']);
            $input['pwd'] = sha1($input['pwd']);
            $map[] = ['id', '=', $input['id']];
            $map[] = ['store_id', '=', $rtn['store_id']];
            $map[] = ['pid', '=', $rtn['id']];
            $map[] = ['type', '=', 6];
            $rtn = Db::table(self::$_baseTable)->where($map)->update($input);
            if ($rtn) return jsonSuccess('修改成功');
            if ($rtn === 0) {
                return jsonFail('当前密码与已有密码一致，更新失败');
            }
            return jsonFail('修改失败');
        } catch (E $e) {
            return jsonFail('服务内部错误~');
        }
    }
}